﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace ExercisesForExtensions
{
    public static class Extensions
    {
        //extension for StringBuilder
        public static StringBuilder Substring(this StringBuilder stringBuilder, int index, int length)
        {
            if (stringBuilder == null)
            {
                throw new ArgumentNullException();
            }

            string newString = stringBuilder.ToString().Substring(index, length);
            StringBuilder result = new StringBuilder();
            result.Append(newString);

            return result;
        }

        //extension for String
        public static String ToTitleCase(this String value)
        {
            if (value == null)
            {
                throw new ArgumentNullException();
            }

            TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;
            return textInfo.ToTitleCase(value);
        }

        //extension for Generic Collection
        public static dynamic Sum<T>(this IEnumerable<T> collection)
        {
            if (collection == null)
            {
                throw new ArgumentNullException();
            }

            dynamic sum = 0;
            foreach (T item in collection)
            {
                sum += item;
            }
            return sum;
        }

        //extension for Generic Collection
        public static dynamic Product<T>(this IEnumerable<T> collection)
        {
            if (collection == null)
            {
                throw new ArgumentNullException();
            }

            dynamic product = 1;
            foreach (T item in collection)
            {
                product *= item;
            }
            return product;
        }

        //extension for Generic Collection
        public static dynamic Average<T>(this IEnumerable<T> collection)
        {
            if (collection == null)
            {
                throw new ArgumentNullException();
            }

            dynamic sum = collection.Sum<T>();
            dynamic count = collection.Count<T>();
            return sum / count;
        }

        //extension for Generic Collection
        public static dynamic Min<T>(this IEnumerable<T> collection)
        {
            if (collection == null)
            {
                throw new ArgumentNullException();
            }

            dynamic min = Int64.MaxValue;
            foreach (T item in collection)
            {
                if (min > item)
                {
                    min = item;
                }
            }
            return min;
        }

        //extension for Generic Collection
        public static dynamic Max<T>(this IEnumerable<T> collection)
        {
            if (collection == null)
            {
                throw new ArgumentNullException();
            }

            dynamic max = Int64.MinValue;
            foreach (T item in collection)
            {
                if (max < item)
                {
                    max = item;
                }
            }
            return max;
        }
    }
}
